package br.com.dallasmotopecas.comum.cliente.controller;

import java.sql.Connection;

import javax.faces.context.ExternalContext;

import br.com.dallasmotopecas.comum.cliente.model.ClienteAbcModel;
import br.com.dallasmotopecas.comum.cliente.vo.ClienteAbcVo;
import br.com.dallasmotopecas.util.controller.AbstractController;
import br.com.dallasmotopecas.util.exception.AlterarObjetoException;
import br.com.dallasmotopecas.util.exception.CampoObrigatorioException;
import br.com.dallasmotopecas.util.exception.ClasseNaoEncontradaException;
import br.com.dallasmotopecas.util.exception.ConexaoException;
import br.com.dallasmotopecas.util.exception.ConsultarObjetoException;

public class ClienteAbcController extends AbstractController
{
	public ClienteAbcController(Connection connection) throws ConexaoException
	{
		super(connection);
	}

	public ClienteAbcController(ExternalContext externalContext) throws ClasseNaoEncontradaException, ConexaoException
	{
		super(externalContext);
	}
	
	private void campoObrigatorio(final ClienteAbcVo clienteAbcVo) throws CampoObrigatorioException
	{
		if (clienteAbcVo.getPercentualCurvaA() <= 0)
			throw new CampoObrigatorioException("Por Favor Informe Percentual Curva A");
		
		if (clienteAbcVo.getPercentualCurvaB() <= 0)
			throw new CampoObrigatorioException("Por Favor Informe Percentual Curva B");
		
		if (clienteAbcVo.getPercentualCurvaC() <= 0)
			throw new CampoObrigatorioException("Por Favor Informe Percentual Curva C");
		
		if (clienteAbcVo.getMargemErroCurvaA() <= 0)
			throw new CampoObrigatorioException("Por Favor Informe Margem Percentual Curva A");
		
		if (clienteAbcVo.getMargemErroCurvaB() <= 0)
			throw new CampoObrigatorioException("Por Favor Informe Margem Percentual Curva B");
		
		if (clienteAbcVo.getMargemErroCurvaC() <= 0)
			throw new CampoObrigatorioException("Por Favor Informe Margem Percentual Curva C");
	}
	
	public void alterarClienteAbc(final ClienteAbcVo clienteAbcVo) throws CampoObrigatorioException, AlterarObjetoException
	{
		ClienteAbcModel clienteAbcModel = null;
		try
		{
			if (clienteAbcVo != null)
			{
				campoObrigatorio(clienteAbcVo);
				clienteAbcModel = new ClienteAbcModel(getConnection());
				
				synchronized (ClienteAbcVo.class)
				{
					beginTransaction();
					clienteAbcModel.alterarCurvaAbc(clienteAbcVo);
					commit();
				}
			}
		}
		catch(AlterarObjetoException e)
		{
			rollback();
			throw e;
		}
		finally
		{
			clienteAbcModel = null;
		}
	}
	
	public void consultarClienteAbc(final ClienteAbcVo clienteAbcVo) throws ConsultarObjetoException
	{
		ClienteAbcModel clienteAbcModel = null;
		try
		{
			if (clienteAbcVo != null)
			{
				clienteAbcModel = new ClienteAbcModel(getConnection());
				clienteAbcModel.consultaClienteAbc(clienteAbcVo);
			}
		}
		catch(ConsultarObjetoException e)
		{
			throw e;
		}
		finally
		{
			clienteAbcModel = null;
		}
	}

}
